import {showNotify, showSuccessToast, showConfirmDialog} from 'vant'
import api from '../../http/api'
import router from '../../router'
import useUtils from '../../hooks/useUtils'

const ROLE_DOCTOR = 1
const ROLE_VOL = 2
const ROLE_DBM = 3
const ROLE_RPM = 4
const SET_ACCOUNT_INFO = 'SET_ACCOUNT_INFO'
export default {
    namespaced: true,
    state: {
        loginForm: {
            mobile: '',
            password: '',
            openid: ''
        },
        accountInfo: {}
    },

    getters: {

	},

    mutations: {
        SET_ACCOUNT_INFO: (state, info) => {
			state.accountInfo = info
		},
    },

    actions: {
        login({ dispatch, commit, state}) {
			const openid = sessionStorage.getItem('openid')
            api.login({
                ...state.loginForm,
                openid
            }).then(res => {
                if(!res.code) {
                    if (res.data) {
                        sessionStorage.setItem('token', res.data.token)
                        commit(SET_ACCOUNT_INFO, res.data)
                    }
                    if (res.data.role == ROLE_DOCTOR) {
                        commit('doctor/SET_INFO', res.data.accountInfo, { root: true })
                        const { approveStatus } = res.data.accountInfo
                        if (approveStatus == 2) {
                            router.replace('/doctor/home');
                        } else if(approveStatus == 1){
                            router.replace('/doctornotice');
                        } else if (approveStatus == 3) {
                            router.replace('/register?update=true');
                        }
                    } else if(res.data.role == ROLE_VOL) {//志愿者
                        commit('vol/SET_INFO', res.data.accountInfo, { root: true })
                        router.replace('/vol');
                    } else if(res.data.role == ROLE_DBM) {//DBM
                        commit('dbm/SET_INFO', res.data.accountInfo, { root: true })
                        router.replace('/dbm');
                    } else if(res.data.role == ROLE_RPM) {//RPM
                        commit('rpm/SET_INFO', res.data.accountInfo, { root: true })
                        router.replace('/rpm');
                    }

					dispatch('getAccountInfoByToken')
                }
            }).catch(() => {})
        },
        loginByOpenid({commit, state}) {
            const openid = localStorage.getItem('rpmAccount') || sessionStorage.getItem('openid')
            api.openidLogin(openid).then(res => {
                if(!res.code) {
                    // if(!res.data) {
                    //     router.replace('/login');
                    // }
                    // 医生
                    if (res.data) {
                        sessionStorage.setItem('token', res.data.token)
                        commit(SET_ACCOUNT_INFO, res.data)
                    }
                    if (res.data.role == ROLE_DOCTOR) {
                        commit('doctor/SET_INFO', res.data.accountInfo, {root:true})
                        const { approveStatus } = res.data.accountInfo
                        if (approveStatus == 2) {
                            router.replace('/doctor/home');
                        } else if(approveStatus == 1 || approveStatus == 4){
                            router.replace('/doctornotice');
                        } else if (approveStatus == 3) {
                            router.replace('/doctornotice');
                        }
                    } else if(res.data.role == ROLE_VOL) {//志愿者
                        commit('vol/SET_INFO', res.data.accountInfo, {root:true})
                        router.replace('/vol');
                    } else if(res.data.role == ROLE_DBM) {//DBM
                        commit('dbm/SET_INFO', res.data.accountInfo, { root: true })
                        router.replace('/dbm');
                    } else if(res.data.role == ROLE_RPM) {//RPM
                        commit('rpm/SET_INFO', res.data.accountInfo, { root: true })
                        router.replace('/rpm');
                    }
                }
            }).catch(() => {})
        },
        logout({commit, state}) {
            showConfirmDialog({
                title: '提示',
                message:
                '确定退出登录么？',
            }).then(() => {
                api.logout().then(res => {
                    if(res.code === 0){
                        router.replace("/login");
						sessionStorage.removeItem('doctorInfo')
						sessionStorage.removeItem('token')
                        localStorage.removeItem('rpmAccount')
                    }
                })
            }).catch(() => {
            })
        },
        getAccountInfoByToken({commit, state}) {
            api.getAccountInfoByToken().then(res => {
                if (res.data) {
                    commit(SET_ACCOUNT_INFO, res.data)
                }
                if (res.data.role == ROLE_DOCTOR) {
                    commit('doctor/SET_INFO', res.data.accountInfo, {root:true})
                } else if(res.data.role == ROLE_VOL) {//志愿者
                    commit('vol/SET_INFO', res.data.accountInfo, {root:true})
                } else if(res.data.role == ROLE_DBM) {//DBM
                    commit('dbm/SET_INFO', res.data.accountInfo, { root: true })
                } else if(res.data.role == ROLE_RPM) {//RPM
                    commit('rpm/SET_INFO', res.data.accountInfo, { root: true })
                }
            })
        }
    },
}
